package com.itheima.controller;

import com.itheima.domain.Book;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;
    @GetMapping
    public Result getAll() {
        return new Result(Code.GET_OK, bookService.getAll(), "查询成功");
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {

        return new Result(Code.GET_ERR, bookService.getById(id), "查询成功");
    }
    @PostMapping
    public Result save(@RequestBody Book book) {
        boolean b = bookService.save(book);
        return new Result(b ? Code.SAVE_OK : Code.SAVE_ERR, null,
                b ? "添加成功" : "添加失败");
    }
    @PutMapping
    public Result update(@RequestBody Book book) {
        boolean b = bookService.save(book);
        return new Result(b ? Code.UPDATE_OK : Code.UPDATE_ERR, null,
                b ? "修改成功" : "修改失败");
    }
    @DeleteMapping("/{id}")
    public Result delete(@RequestBody Integer id) {
        boolean b = bookService.delete(id);
        return new Result(b ? Code.DELETE_OK : Code.DELETE_ERR, null,
                b ? "删除成功" : "删除失败");
    }
}
